var prefix = jsctx + "/orderDiscount";

var operationType = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#exampleTable').bootstrapTable('refresh');
    };

    function reset() {
        $("#discountType").val('');
        $("#orderNum").val("");
        $("#discountNum").val("");
        $("#gunNo").val("");
    }

    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listOrderDiscountRecord",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageList: [10, 20, 30],
                pageSize: 10,
                pageNumber: 1,
                sidePagination: "server",
                cache: false,
                height: h,
                paginationShowPageGo: true,
                rightFixedColumns: false,
                showColumns: true,
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,

                        discountType: $("#discountType").val().trim(),
                        orderNum: $("#orderNum").val().trim(),
                        discountNum: $("#discountNum").val().trim(),
                        gunNo: $("#gunNo").val().trim()
                    };
                },
                columns: [
                    {
                        checkbox: true, field: '', width: '50', align: 'left',
                    },
                    {
                        field: 'number',
                        title: '序号',
                        width: '50', align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    {field: 'orderNum', title: '充电订单号', align: 'center', width: '80'},
                    {field: 'discountNum', title: '优惠单号', width: '100', align: 'center'},
                    {field: 'orderNum', title: '交易流水号', width: '100', align: 'center'},
                    {field: 'gunNo', title: '充电枪编号', width: '100', align: 'center'},
                    {field: 'stationName', title: '站点名称', width: '100', align: 'center'},
                    {field: 'operatorName', title: '所属加盟商', width: '100', align: 'center'},
                    {
                        field: 'discountType', title: '优惠方式', width: '70', align: 'center',
                        formatter: function (value, row, index) {
                            if (value == '0') {
                                return '整单满减优惠券';
                            }  else  if(value == '1'){
                                return '服务费满减优惠卷';
                            }  else  if(value == '2'){
                                return '充电满折';
                            }  else  if(value == '3'){
                                return '服务器满折';
                            }  else  if(value == '4'){
                                return '代金券';
                            }  else  if(value == '5'){
                                return '生日免单';
                            }  else  if(value == '6'){
                                return '生日礼遇减免';
                            }  else  if(value == '7'){
                                return '会员优惠';
                            }  else  if(value == '8'){
                                return '权益金';
                            }  else  if(value == '9'){
                                return '企业优惠';
                            }  else  if(value == '10'){
                                return '活动满减';
                            } else{
                                return '未知';
                            }
                        }
                    },
                    {field: 'discountAmount', title: '优惠金额', width: '50', align: 'center'},
                    {
                        field: 'discountSource', title: '优惠来源', width: '50', align: 'center',
                        formatter: function (value, row, index) {
                            if (value == '0') {
                                return '平台';
                            }  else  if(value == '1'){
                                return '运营商';
                            } else{
                                return '未知';
                            }
                        }
                    },
                    {field: 'occupyPayTime', title: '支付时间', width: '80', align: 'center'},
                    {field: 'account', title: '余额', width: '50', align: 'center'},
                    /*{
                        field: 'giveAccount', title: '权益金(元)', width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            return value + row.frozenGiveAccount;
                        }
                    },
                    {
                        field: 'memberLevel', title: '会员等级', width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            if (row.memberLevel != null && row.memberLevel != '') {
                                return row.memberLevel;
                            }  else  {
                                return '';
                            }
                        }
                    },
                    {
                        field: 'status',
                        title: '状态',
                        width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            if (row.status == '0') {
                                return '<span class="label label-success">正常</span>';
                            } else if (row.status == '1') {
                                return '<span class="label label-danger">停用</span>';
                            }
                        }
                    },*/
                    {
                        title: '操作',
                        field: 'orderDiscountId',
                        width: '50', align: 'center',
                        class: '',
                        formatter: function (value, row, index) {
                            var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="删除" onclick="operationType.updateStatus(\''
                                + row.orderDiscountId
                                + '\')">删除</a> ';
                            return a;


                        }
                    }
                ]
            });
    };

    function exportOrderDiscountRecord() {
        $.fileDownload(prefix + "/exportOrderDiscountRecord", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text(),

                discountType: $("#discountType").val().trim(),
                orderNum: $("#orderNum").val().trim(),
                discountNum: $("#discountNum").val().trim(),
                gunNo: $("#gunNo").val().trim()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    };


    function updateStatus(orderDiscountId) {
        layer.confirm('确定要删除？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/delOrderDiscountRecord",
                type: "post",
                data: {
                    'id': orderDiscountId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function batchHandleRows() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');
        if (rows.length == 0) {
            alert("请先选择要删除的记录!");
            return;
        } else {
            var idList = new Array();
            $(rows).each(function () {
                if (this.orderDiscountId != null){
                    idList.push(this.orderDiscountId);
                }
            });
            layer.open({
                type: 1,
                title: '删除确认',
                closeBtn: false,
                shade: 0.4,
                shift: 7,
                shadeClose: true,
                content: $("#open-main"),
                btn: ['确定', '取消'],
                yes: function (index) {//点击确定回调
                    layer.close(index);
                    $.ajax({
                        url: prefix + "/batchDelOrderDiscountRecord",
                        type: "post",
                        data: {
                            idList: idList,
                        },
                        traditional:true,
                        success: function (data) {
                            layer.close(index);
                            if (data.status == 200) {
                                reLoad();
                                layer.msg(data.message);
                            } else {
                                layer.msg(data.message);
                            }
                        }
                    });
                },
            });
        }
    }

    return {
        init: init,
        reLoad: reLoad,
        reset: reset,
        updateStatus: updateStatus,
        batchHandleRows:batchHandleRows,
        exportOrderDiscountRecord: exportOrderDiscountRecord,
    };

})();

